<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/13
 * Time: 17:59
 */

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class BangdanController extends Controller
{
    //榜单首页
    public function orders(Request $request){
        $pagecanshu = trim($request->pagecanshu);
        if($pagecanshu == ''){
            $pagecanshu = 1;
        }
        $searchname   = trim($request->searchname);
        $searchname   = $searchname?$searchname:"";
        // 1：明日之星  默认0：交易大师（交易大师必须认证通过）
        $grade        = intval($request->grade);
        $grade        = $grade?$grade:0;
        if($grade == 1){
            $where['isanalyst']    = '2';
        }else{
            $where['isanalyst']    = '1';
//            $where['m.authstatus'] = 2; //认证状态
        }
        // 0:段位榜 1:总盈榜  2:胜率榜  3:单均榜
        $bdtype       = intval($request->bdtype);
        $bdtype       = $bdtype?$bdtype:0;
        if($bdtype == 1){
            $orderBy  = 'final_point';
            $sort     = 'desc';
        }elseif($bdtype == 2){
            $orderBy  = 'winrate';
            $sort     = 'desc';
        }elseif($bdtype == 3){
            $orderBy  = 'single_final_point';
            $sort     = 'desc';
        }else{
            $orderBy  = 'grade';
            $sort     = 'asc';
        }
        //0: 全部 1：近一周 2：近一个月 3：近三个月
        $type         = intval($request->type);
        $type         = $type?$type:0;
        if($type == 1){
            $end_start_time = strtotime("-1 week");
        }else if($type == 2){
            $end_start_time = strtotime("-1 month");
        }else if($type == 3){
            $end_start_time = strtotime("-3 month");
        }else{
            $end_start_time = 0;
        }
        $where['o.ispersonal'] = 0; //网站下单的
        $where['o.isstop']     = 0; //已平仓的
        $session_data = $request->session()->get('eai');
        /*DB::connection()->enableQueryLog();
        DB::table('waihui_member');*/
        $list = DB::table('waihui_member as m')
            ->join('waihui_order as o','m.tel','=','o.tel')
            ->select('m.authstatus','m.id','m.grade','m.tel','m.money','m.username','m.isanalyst','m.photo','m.star',DB::raw("(SUM(o.final_point)+COUNT(o.id)*5) AS final_point"),DB::raw("((SUM(o.final_point)+COUNT(o.id)*5)/count(o.tel)) AS single_final_point"),DB::raw("(count(case when o.trade_status='1' then 1 end) / count(o.tel))*100 AS winrate"),DB::raw("count(case when o.trade_status='1' then 1 end) AS wincounts"),DB::raw("count(o.tel) as counts"))
            ->where($where)
            ->whereBetween('o.end_time', [$end_start_time, strtotime('now')])
            ->where('m.username', 'like' , "%$searchname%")
            ->havingRaw('(SUM(o.final_point)+COUNT(o.id)*5) > 0')
            ->havingRaw('COUNT(o.id) > 10')
            ->orderBy($orderBy,$sort)
            ->orderBy('m.star','desc')
            ->orderBy('single_final_point','desc')
            ->groupBy('m.tel')
            ->paginate(10);
        /*$sql =DB::getQueryLog();
        dd($sql);*/
        foreach ($list as $key=>$val){
            $list[$key]->weekordernum = DB::table('waihui_order')->where([['tel','=',$val->tel],['isstop','=',0],['begin_time','>',strtotime("-1 week")],['begin_time','<',strtotime("now")]])->count();//近一周的策略总数
            $list[$key]->monthordernum = DB::table('waihui_order')->where([['tel','=',$val->tel],['isstop','=',0],['begin_time','>',strtotime("-1 month")],['begin_time','<',strtotime("now")]])->count();//近一月的策略总数
            $list[$key]->fansnum      = BangdanController::followCount($val->id); //订阅该分析师的总数
            //获取图标中点位信息
            $trendpointobj            = DB::table('waihui_order')
                                            ->select(DB::raw('(SUM(final_point)+COUNT(id)*5) as final_point'),DB::raw("FROM_UNIXTIME(end_time, '%Y-%m-%d') AS days"))
                                            ->where([['tel','=',$val->tel],['ispersonal','=',0],['isstop','=',0],['end_time','>',$end_start_time],['end_time','<',strtotime("-1 day 23:59:59")]])
                                            ->groupBy('days')->get()->toArray();
            $timearr  = [];
            $pointarr = [];
            $pointcount = 0;
            foreach ($trendpointobj as $tkey=>$tval){
                $timearr[]  = $tval->days;
                $pointcount = $pointcount+$tval->final_point;
                $pointarr[] = round($pointcount,2);
            }
            $list[$key]->timesstr     = implode(',',$timearr);
            $list[$key]->pointstr     = implode(',',$pointarr);
            
            //是否订阅了该分析师
            if($session_data){
                $fllowinfo = DB::table('waihui_follow')->select('status','direction')->where([['analyst_id','=',$val->id],['follower_id','=',$session_data['id']]])->get()->toArray();
                if($fllowinfo){
                    if($fllowinfo[0]->status){
                        $list[$key]->status    = 1;//已订阅
                        $list[$key]->direction = $fllowinfo[0]->direction;//订阅方向
                    }else{
                        $list[$key]->status    = 0;//取消订阅
                        $list[$key]->direction = 0;//取消订阅
                    }
                }else{
                    $list[$key]->status    = 3;//未订阅
                    $list[$key]->direction = 3;//未订阅
                }
            }else{
                $list[$key]->status    = 4; //未登录
                $list[$key]->direction = 4; //未登录
            }
        }
//      dd($list);
        $uid = $session_data['id'];
        $userinfo = DB::table('waihui_member as m')
                        ->select('m.username','m.sex','m.email','m.birthday','m.server_name')
                        ->where('m.id','=',$uid)
                        ->first();
        if(!$userinfo){
            $infofinish = 1; //未完善资料
        }else{
            $userinfos = DB::table('waihui_member_info as i')->select('i.province','i.city','i.country')->where('i.userid','=',$uid)->first();
            if($userinfo->username  && $userinfo->sex  && $userinfo->email && $userinfo->birthday  && $userinfo->server_name  && $userinfos->province && $userinfos->city && $userinfos->country ){
                $infofinish = 0;  //已完善资料
            }else{
                $infofinish = 1; //未完善资料
            }
        }

            $totalbaobiao = $this->allOrder();
            $totalbbtime = $totalbaobiao[0];//报表时间
            $totalbbpoint = $totalbaobiao[1];//报表收益
            $totalbballpoint = $totalbaobiao[2];//报表总收益点

        if(isMobile()){
            return  view('newmobile.qianlibang',compact('list','searchname','type','grade','bdtype','infofinish','totalbbtime','totalbbpoint','totalbballpoint'));
        }else{
            return  view('bangdan.index',compact('list','searchname','type','grade','bdtype','infofinish','totalbbtime','totalbbpoint','totalbballpoint','pagecanshu'));
        }
    }
    //榜单页顶部数据图
    public function allOrder(){
//        $where['isanalyst']     = '1';
        $where['o.ispersonal']  = 0; //网站下单的
        $where['o.isstop']      = 0; //已平仓的
        $tellist = DB::table('waihui_member as m')
            ->join('waihui_order as o','m.tel','=','o.tel')
            ->select('m.tel')
            ->where($where)
            ->whereBetween('o.end_time', [0, strtotime('now')])
            ->havingRaw('(SUM(o.final_point)+COUNT(o.id)*5) > 0')
            ->havingRaw('COUNT(o.id) > 15')
            ->groupBy('m.tel')
            ->pluck('m.tel');
//        DB::connection()->enableQueryLog();
        $result = DB::table('waihui_order')->select(DB::raw("(SUM(final_point)+COUNT(id)*5) as final_point"),"end_time",DB::raw("FROM_UNIXTIME(end_time, '%Y-%m-%d') as datetime"))->whereIn('tel',$tellist)->groupBy('datetime')->orderBy('datetime','asc')->get()->toArray();
//        $sql =DB::getQueryLog();
//        dd($sql);
        $total = 0;
        $timeDay = [];
        $pointarr = [];
        if($result){
            foreach($result as $k=>$v){
                $timeDay[] = $result[$k]->datetime;
                $total = $total +$result[$k]->final_point;
                $pointarr[] = $total;
            }
            $timeDayJiaoyi = implode(',',$timeDay);
            $pointDayJiaoyi = implode(',',$pointarr);
        }else{
            $timeDayJiaoyi = '';
            $pointDayJiaoyi = '';
        }
        $arr = array($timeDayJiaoyi,$pointDayJiaoyi,$total);
        return $arr;
    }
  //订阅该分析师的总人数
  public static function followCount($userid){
      return DB::table('waihui_follow')->where([['analyst_id','=',$userid],['status','=',1]])->count();
  }

  // 新版分析师榜-->顶部总收益曲线图
  // public function allProfit(){
  //       $totalbaobiao = $this->allOrder();
  //       $totalbbtime = $totalbaobiao[0];//报表时间
  //       $totalbbpoint = $totalbaobiao[1];//报表收益
  //       $totalbballpoint = $totalbaobiao[2];//报表总收益点
  //     return view('new.allprofit',compact('totalbaobiao','totalbbtime','totalbbpoint','totalbballpoint'));
  // }

  //新版分析师榜--->潜力榜
  public function qianLiBang(Request $request){
        $session_data = $request->session()->get('eai');
        $uid = $session_data['id'];
        $userinfo = DB::table('waihui_member as m')
                        ->select('m.username','m.sex','m.email','m.birthday','m.server_name')
                        ->where('m.id','=',$uid)
                        ->first();
        if(!$userinfo){
            $infofinish = 1; //未完善资料
        }else{
            $userinfos = DB::table('waihui_member_info as i')->select('i.province','i.city','i.country')->where('i.userid','=',$uid)->first();
            if($userinfo->username  && $userinfo->sex  && $userinfo->email && $userinfo->birthday  && $userinfo->server_name  && $userinfos->province && $userinfos->city && $userinfos->country ){
                $infofinish = 0;  //已完善资料
            }else{
                $infofinish = 1; //未完善资料
            }
        }

//        $weekstarttime  = strtotime(date("Y-m-d H:i:s",mktime(6,0,0,date("m"),date("d")-date("w")+(date("w")==0?-6:1),date("Y")))); //周一早上6:00:00
//        $weekendtime    = strtotime(date("Y-m-d H:i:s",mktime(12,0,0,date("m"),date("d")-date("w")+(date("w")==0?-1:6),date("Y")))); //周六早上5:00:00
        $endTime   = time();
        $beginTime = $endTime - 7*24*3600;
        //dd($endTime);
        $where['o.isstop']     = 0; //已平仓
        $where['o.ispersonal'] = 0; //网站下单的

        $searchname     = trim($request->searchname);
        $searchname     = $searchname?$searchname:"";
        $pagecanshu = trim($request->pagecanshu);
        if($pagecanshu == ''){
            $pagecanshu = 1;
        }
        if($searchname){
            $list = DB::table('waihui_member as m')
                            ->leftJoin('waihui_order as o', 'm.tel', 'o.tel')
                            ->select('m.id', 'm.tel', 'm.username', 'm.authstatus', 'm.photo', DB::raw('(SUM(o.final_point)+COUNT(o.id)*5) as week_final_point'), DB::raw('count(o.tel) as ordernum'), DB::raw("((SUM(o.final_point)+COUNT(o.id)*5)/count(o.tel)) AS single_final_point"), DB::raw("(count(case when o.trade_status='1' then 1 end) / count(o.tel))*100 AS winrate"), DB::raw("ROUND(SUM(CASE WHEN o.trade_status='1' THEN (final_point+5) END),2) AS profit_final_point"),DB::raw("ROUND(SUM(CASE WHEN o.trade_status !='1' THEN (final_point+5) END),2) AS loss_final_point"))
                            ->where($where)
                            ->whereBetween('o.end_time', [$beginTime, $endTime])
                            ->orderBy('week_final_point', 'desc')
                            ->orderBy('single_final_point', 'desc')
                            ->groupBy('m.id')
                            ->paginate(10);
            foreach ($list as $key=>$val){
                if($val->username == $searchname){
                    $list[$key] = $val;
                }else{
                    unset($list[$key]);
                }
            }
        }else {
             DB::connection()->enableQueryLog();
            $list = DB::table('waihui_member as m')
                            ->leftJoin('waihui_order as o', 'm.tel', 'o.tel')
                            ->select('m.id', 'm.tel', 'm.username', 'm.authstatus', 'm.photo', DB::raw('(SUM(o.final_point)+COUNT(o.id)*5) as week_final_point'), DB::raw('count(o.tel) as ordernum'), DB::raw("((SUM(o.final_point)+COUNT(o.id)*5)/count(o.tel)) AS single_final_point"), DB::raw("(count(case when o.trade_status='1' then 1 end) / count(o.tel))*100 AS winrate"), DB::raw("ROUND(SUM(CASE WHEN o.trade_status='1' THEN (final_point+5) END),2) AS profit_final_point"),DB::raw("ROUND(SUM(CASE WHEN o.trade_status !='1' THEN (final_point+5) END),2) AS loss_final_point"))
                            ->where($where)
                            ->whereBetween('o.end_time', [$beginTime, $endTime])
                            ->orderBy('week_final_point', 'desc')
                            ->orderBy('single_final_point', 'desc')
                            ->groupBy('m.id')
//                            ->get()->toArray();
                            ->paginate(10);
//            $list = array_slice($list,0,50);
            
        }
        // dd($list);
        foreach ($list as $key=>$val){
            // 盈亏比
            $list[$key]->bili = ($val -> profit_final_point == null ? 0:$val -> profit_final_point).':'.($val-> loss_final_point == null? 0:$val-> loss_final_point);
            //获取图标中点位信息
            $trendpointobj            = DB::table('waihui_order')
                                            ->select(DB::raw('(final_point+5) as final_point'),'end_time')
                                            ->where([['tel','=',$val->tel],['ispersonal','=',0],['isstop','=',0],['end_time','>=',$beginTime],['end_time','<=',$endTime]])
                                            ->orderBy('end_time','desc')->get()->toArray();
            $timearr  = [];
            $pointarr = [];
            $pointcount = 0;
            foreach ($trendpointobj as $tkey=>$tval){
                $timearr[]  = date('Y-m-d',$tval->end_time);
                $pointcount = $pointcount+$tval->final_point;
                $pointarr[] = round($pointcount,2);
            }
            $list[$key]->timesstr     = implode(',',$timearr);
            $list[$key]->pointstr     = implode(',',$pointarr);
            
            //是否订阅了该分析师
            if($session_data){
                $fllowinfo = DB::table('waihui_follow')->select('status','direction')->where([['analyst_id','=',$val->id],['follower_id','=',$session_data['id']]])->get()->toArray();
                if($fllowinfo){
                    if($fllowinfo[0]->status){
                        $list[$key]->status    = 1;//已订阅
                        $list[$key]->direction = $fllowinfo[0]->direction;//订阅方向
                    }else{
                        $list[$key]->status    = 0;//取消订阅
                        $list[$key]->direction = 0;//取消订阅
                    }
                }else{
                    $list[$key]->status    = 3;//未订阅
                    $list[$key]->direction = 3;//未订阅
                }
            }else{
                $list[$key]->status    = 4; //未登录
                $list[$key]->direction = 4; //未登录
            }
        }
// dd($list);
        $totalbaobiao = $this->allOrder();
        $totalbbtime = $totalbaobiao[0];//报表时间
        $totalbbpoint = $totalbaobiao[1];//报表收益
        $totalbballpoint = $totalbaobiao[2];//报表总收益点
        if(isMobile()){
            return view('newmobile.index',compact('infofinish','totalbaobiao','totalbbtime','totalbbpoint','totalbballpoint','list','searchname','pagecanshu'));
        }else{
            return view('new.qianlibang',compact('infofinish','totalbaobiao','totalbbtime','totalbbpoint','totalbballpoint','list','searchname','pagecanshu'));
        }
  }
  // 连胜榜
    public function lianshengBang(Request $request){
        $session_data = $request->session()->get('eai');
        $uid = $session_data['id'];
        
        //查看用户是否完善资料
        $userinfo = DB::table('waihui_member as m')
            ->select('m.username','m.sex','m.email','m.birthday','m.server_name')
            ->where('m.id','=',$uid)
            ->first();
        if(!$userinfo){
            $infofinish = 1; //未完善资料
        }else{
            $userinfos = DB::table('waihui_member_info as i')->select('i.province','i.city','i.country')->where('i.userid','=',$uid)->first();
            if($userinfo->username  && $userinfo->sex  && $userinfo->email && $userinfo->birthday  && $userinfo->server_name  && $userinfos->province && $userinfos->city && $userinfos->country ){
                $infofinish = 0;  //已完善资料
            }else{
                $infofinish = 1; //未完善资料
            }
        }
//        $weekstarttime  = strtotime(date("Y-m-d H:i:s",mktime(6,0,0,date("m"),date("d")-date("w")+(date("w")==0?-6:1),date("Y")))); //周一早上6:00:00
//        $weekendtime    = strtotime(date("Y-m-d H:i:s",mktime(12,0,0,date("m"),date("d")-date("w")+(date("w")==0?-1:6),date("Y")))); //周六早上5:00:00
        $weekendtime   = strtotime(date('Y-m-d 00:00:00',time()));
        $weekstarttime = $weekendtime - 7*24*3600;
        
        
        $searchname     = trim($request->searchname); 
        $searchname     = $searchname ? $searchname : ""; //用户名搜索条件
        $pagecanshu = trim($request->pagecanshu);         //页面上左侧条形或者右侧块形
        if($pagecanshu == ''){
            $pagecanshu = 1;
        }
        $pagesize = 10;
        $page = intval(trim($request->page)) ? intval(trim($request->page)) : 1;
        
        $totalbaobiao = $this->allOrder();
        $totalbbtime = $totalbaobiao[0];//报表时间
        $totalbbpoint = $totalbaobiao[1];//报表收益
        $totalbballpoint = $totalbaobiao[2];//报表总收益点
        $cachedata = Cache::get('liansheng');
        if(!$cachedata || $searchname != ''){
            //查询所有的已认证的分析师
            $memberData = DB::table('waihui_member as m')
                          ->join('waihui_order as o','m.tel','o.tel')
                          ->select('m.liansheng','m.authstatus','m.id','m.grade','m.tel','m.username','m.isanalyst','m.photo','m.star')
    //                      ->where([['isanalyst','=','1']])
                          ->where('username', 'like' , "%$searchname%")
                          ->whereBetween('o.end_time', [$weekstarttime, $weekendtime])
    //                      ->havingRaw('(SUM(o.final_point)+COUNT(o.id)*5) > 0')
    //                      ->havingRaw('COUNT(o.id) > 15')
                          ->groupBy('m.tel')
                          ->get()->toArray();

            foreach($memberData as $k=>$v){
                $memberData[$k]->monthordernum = DB::table('waihui_order')->where([['tel','=',$v->tel],['isstop','=',0],['begin_time','>',strtotime("-1 month")],['begin_time','<',strtotime("now")]])->count();//近一月的策略总数
    //            if($memberData[$k]->liansheng == 0){ //如果member表的liansheng字段是0的话，那么就查询该用户是不是因为定级长星的原因
                    $liansheng = 0; //先定义一个连胜默认值
                    $point     = 0; //先定义一个连胜的总点位默认值
                    $tradeid   = '';
                    for($i=0; $i<50; $i++){
                        $firstData =  DB::table('waihui_order')
                                    ->select('trade_status','final_point','tradeid')
                                    ->where([['isstop','=',0],['ispersonal','=',0],['tel','=',$v->tel]])
                                    ->orderBy('end_time','desc')
                                    ->offset("$i")
                                    ->first();

                        if($firstData != '' && $firstData->trade_status == '1'){
                            $point += $firstData->final_point;
                            $liansheng++;
                            $tradeid .= $firstData->tradeid.',';
                            continue;
                        }else{ //如果用户按平仓时间倒序排序的第一单是亏损的话，那么该用户的连胜为0
                            break;
                        }

                    }
                    $memberData[$k]->liansheng = $liansheng;
                    $memberData[$k]->profit = $point;
                    $memberData[$k]->tradeid = rtrim($tradeid,',');
    //            }else{
    //                $profit = 0;
    //                $kvtradeid = '';
    //                $data = DB::select('select final_point,tradeid from waihui_order where tel = "'.$v->tel.'" and isstop =0 and ispersonal =0 ORDER BY end_time desc LIMIT '.$v->liansheng);
    //                foreach($data as $dk=>$dv){
    //                    $profit  += $dv->final_point;
    //                    $kvtradeid .= $dv->tradeid.',';
    //                }
    //                $memberData[$k]->profit = $profit;
    //                $memberData[$k]->tradeid = rtrim($kvtradeid , ',');
    //            }

                //连胜单均盈利点位
                if($v->liansheng != 0){
                     $memberData[$k]->avgrate = round($memberData[$k]->profit/$v->liansheng,2);
                }else{
                    $memberData[$k]->avgrate = 0;
                }
                //连胜涨幅
                $zhangfu = DB::select('select sum(final_point) as allpoint from waihui_order where tel = "'.$v->tel.'" and isstop =0 and ispersonal =0');
                $lastPoint = $zhangfu[0]->allpoint - $memberData[$k]->profit;
                $memberData[$k]->zhangfu = round($memberData[$k]->profit/($lastPoint == 0 ? 1 : abs($lastPoint)),2)*100;

                /*//是否订阅了该分析师
                if($session_data){
                    $fllowinfo = DB::table('waihui_follow')->select('status','direction')->where([['analyst_id','=',$v->id],['follower_id','=',$session_data['id']]])->get()->toArray();
                    if($fllowinfo){
                        if($fllowinfo[0]->status){
                            $memberData[$k]->status    = 1;//已订阅
                            $memberData[$k]->direction = $fllowinfo[0]->direction;//订阅方向
                        }else{
                            $memberData[$k]->status    = 0;//取消订阅
                            $memberData[$k]->direction = 0;//取消订阅
                        }
                    }else{
                        $memberData[$k]->status    = 3;//未订阅
                        $memberData[$k]->direction = 3;//未订阅
                    }
                }else{
                    $memberData[$k]->status    = 4; //未登录
                    $memberData[$k]->direction = 4; //未登录
                }*/
            }

            //去除连胜为0的元素
            foreach($memberData as $kk=>$kv){
                if($kv->liansheng == 0 || $kv->liansheng == 1){
                    unset($memberData[$kk]);
                }
            }
            //dd($memberData);
            //排序，按连胜字段由大到小排序
            $list = $this->list_order($memberData,'liansheng','desc','number');
            if($searchname == ''){
                Cache::put('liansheng',$list, 60); //缓存一小时
            }
        }else{
             $list = $cachedata;
        }
        $allpage = ceil(count($list)/$pagesize); //总页数
        $memberData = array_slice($list,($page-1)*$pagesize ,$pagesize);
        foreach ($memberData as $k=>$v){
            if($session_data){
                $fllowinfo = DB::table('waihui_follow')->select('status','direction')->where([['analyst_id','=',$v->id],['follower_id','=',$session_data['id']]])->get()->toArray();
                if($fllowinfo){
                    if($fllowinfo[0]->status){
                        $memberData[$k]->status    = 1;//已订阅
                        $memberData[$k]->direction = $fllowinfo[0]->direction;//订阅方向
                    }else{
                        $memberData[$k]->status    = 0;//取消订阅
                        $memberData[$k]->direction = 0;//取消订阅
                    }
                }else{
                    $memberData[$k]->status    = 3;//未订阅
                    $memberData[$k]->direction = 3;//未订阅
                }
            }else{
                $memberData[$k]->status    = 4; //未登录
                $memberData[$k]->direction = 4; //未登录
            }
        }
        // dd($memberData);
        if(isMobile()){
            return  view('newmobile.lianshengbang',compact('page','allpage','memberData','totalbaobiao','totalbbtime','totalbbpoint','totalbballpoint','searchname','pagecanshu','infofinish'));
        }else{
            return  view('new.lianshengbang',compact('page','allpage','memberData','totalbaobiao','totalbbtime','totalbbpoint','totalbballpoint','searchname','pagecanshu','infofinish'));
        }
        
    }
    
    /**
     * 对二维数组进行排序
     * 模拟 数据表记录按字段排序
     *
     * <code>
     *  @list_order($list, $get['orderKey'], $get['orderType']);
     * </code>
     * @param array $array 要排序的数组
     * @param string $orderKey 排序关键字/字段
     * @param string $orderType 排序方式，'asc':升序，'desc':降序
     * @param string $orderValueType 排序字段值类型，'number':数字，'string':字符串
     * @link http://www.cnblogs.com/52php/p/5668809.html 

     */
    function list_order($array, $orderKey, $orderType = 'asc', $orderValueType = 'string') {
        if (is_array($array)) {
            $orderArr = array();
            foreach ($array as $val) {
                $orderArr[] = $val->$orderKey;
            }
            $orderType = ($orderType == 'asc') ? SORT_ASC : SORT_DESC;
            $orderValueType = ($orderValueType == 'string') ? SORT_STRING : SORT_NUMERIC;
            array_multisort($orderArr, $orderType, $orderValueType, $array);
            return $array;
        }
    }




    // 新版段位榜
    public function duanWeiBang(Request $request){
       $pagecanshu = trim($request->pagecanshu);
       if($pagecanshu == ''){
           $pagecanshu = 1;
       }
       $searchname   = trim($request->searchname);
       $searchname   = $searchname?$searchname:"";
//       $where['isanalyst']    = '1';  //是分析师
       // 0:段位榜

       $orderBy  = 'grade';
       $sort     = 'asc';
       $end_start_time = 0;
       $where['o.ispersonal'] = 0; //网站下单的
       $where['o.isstop']     = 0; //已平仓的
       $session_data = $request->session()->get('eai');
       /*DB::connection()->enableQueryLog();
       DB::table('waihui_member');*/
       $list = DB::table('waihui_member as m')
           ->join('waihui_order as o','m.tel','=','o.tel')
           ->select('m.authstatus','m.id','m.grade','m.tel','m.money','m.username','m.isanalyst','m.photo','m.star',DB::raw("(SUM(o.final_point)+COUNT(o.id)*5) AS final_point"),DB::raw('count(o.tel) as ordernum'),DB::raw("((SUM(o.final_point)+COUNT(o.id)*5)/count(o.tel)) AS single_final_point"),DB::raw("(count(case when o.trade_status='1' then 1 end) / count(o.tel))*100 AS winrate"),DB::raw("count(case when o.trade_status='1' then 1 end) AS wincounts"),DB::raw("count(o.tel) as counts"))
           ->where($where)
           ->whereBetween('o.end_time', [$end_start_time, strtotime('now')])
           ->where('m.username', 'like' , "%$searchname%")
           ->havingRaw('(SUM(o.final_point)+COUNT(o.id)*5) > 0')
           ->havingRaw('COUNT(o.id) > 10')
           ->orderBy($orderBy,$sort)
           ->orderBy('m.star','desc')
           ->orderBy('single_final_point','desc')
           ->groupBy('m.tel')
           ->paginate(10);
       /*$sql =DB::getQueryLog();
       dd($sql);*/
       foreach ($list as $key=>$val){
           $list[$key]->weekordernum = DB::table('waihui_order')->where([['tel','=',$val->tel],['isstop','=',0],['begin_time','>',strtotime("-1 week")],['begin_time','<',strtotime("now")]])->count();//近一周的策略总数
           $list[$key]->monthordernum = DB::table('waihui_order')->where([['tel','=',$val->tel],['isstop','=',0],['begin_time','>',strtotime("-1 month")],['begin_time','<',strtotime("now")]])->count();//近一月的策略总数
           //获取图标中点位信息
           $trendpointobj            = DB::table('waihui_order')
               ->select(DB::raw('(SUM(final_point)+COUNT(id)*5) as final_point'),DB::raw("FROM_UNIXTIME(end_time, '%Y-%m-%d') AS days"))
               ->where([['tel','=',$val->tel],['ispersonal','=',0],['isstop','=',0],['end_time','>',$end_start_time],['end_time','<',strtotime("-1 day 23:59:59")]])
               ->groupBy('days')->get()->toArray();
           $timearr  = [];
           $pointarr = [];
           $pointcount = 0;
           foreach ($trendpointobj as $tkey=>$tval){
               $timearr[]  = $tval->days;
               $pointcount = $pointcount+$tval->final_point;
               $pointarr[] = round($pointcount,2);
           }
           $list[$key]->timesstr     = implode(',',$timearr);
           $list[$key]->pointstr     = implode(',',$pointarr);

           //是否订阅了该分析师
           if($session_data){
               $fllowinfo = DB::table('waihui_follow')->select('status','direction')->where([['analyst_id','=',$val->id],['follower_id','=',$session_data['id']]])->get()->toArray();
               if($fllowinfo){
                   if($fllowinfo[0]->status){
                       $list[$key]->status    = 1;//已订阅
                       $list[$key]->direction = $fllowinfo[0]->direction;//订阅方向
                   }else{
                       $list[$key]->status    = 0;//取消订阅
                       $list[$key]->direction = 0;//取消订阅
                   }
               }else{
                   $list[$key]->status    = 3;//未订阅
                   $list[$key]->direction = 3;//未订阅
               }
           }else{
               $list[$key]->status    = 4; //未登录
               $list[$key]->direction = 4; //未登录
           }
       }
//      dd($list);
       $uid = $session_data['id'];
       $userinfo = DB::table('waihui_member as m')
           ->select('m.username','m.sex','m.email','m.birthday','m.server_name')
           ->where('m.id','=',$uid)
           ->first();
       if(!$userinfo){
           $infofinish = 1; //未完善资料
       }else{
           $userinfos = DB::table('waihui_member_info as i')->select('i.province','i.city','i.country')->where('i.userid','=',$uid)->first();
           if($userinfo->username  && $userinfo->sex  && $userinfo->email && $userinfo->birthday  && $userinfo->server_name  && $userinfos->province && $userinfos->city && $userinfos->country ){
               $infofinish = 0;  //已完善资料
           }else{
               $infofinish = 1; //未完善资料
           }
       }
       $totalbaobiao = $this->allOrder();
       $totalbbtime = $totalbaobiao[0];//报表时间
       $totalbbpoint = $totalbaobiao[1];//报表收益
       $totalbballpoint = $totalbaobiao[2];//报表总收益点
       if(isMobile()){
           return  view('newmobile.duanweibang',compact('list','searchname','type','grade','bdtype','infofinish','totalbbtime','totalbbpoint','totalbballpoint'));
       }else{
           return  view('new.duanweibang',compact('list','searchname','type','grade','bdtype','infofinish','totalbbtime','totalbbpoint','totalbballpoint','pagecanshu'));
       }
  }
    // 稳定指数
    public function wenDingZhiShu(Request $request){
        $pagecanshu = trim($request->pagecanshu);
        if($pagecanshu == ''){
            $pagecanshu = 1;
        }
        $searchname   = trim($request->searchname);
        $searchname   = $searchname?$searchname:"";
//        $where['isanalyst']    = '1';  
        
        $orderBy  = 'wenDing_fengXian';
        $sort     = 'desc';
        
        
        $where['o.ispersonal'] = 0; //网站下单的
        $where['o.isstop']     = 0; //已平仓的
        $session_data = $request->session()->get('eai');

        $list = DB::table('waihui_member as m')
            ->join('waihui_order as o','m.tel','=','o.tel')
            ->select('m.authstatus','m.id','m.grade','m.tel','m.money','m.username','m.isanalyst','m.photo','m.star',
                    DB::raw("(SUM(o.final_point)+COUNT(o.id)*5) AS final_point"),
                    DB::raw("((SUM(o.final_point)+COUNT(o.id)*5)/count(o.tel)) AS single_final_point"),
                    DB::raw("(count(case when o.trade_status='1' then 1 end) / count(o.tel))*100 AS winrate"),
                    DB::raw("count(case when o.trade_status='1' then 1 end) AS wincounts"),
                    DB::raw("count(o.tel) as counts"),
                    DB::raw("round((sum(case when o.trade_status='1' then final_point+5 end) / (sum(case when o.trade_status='1' then final_point+5 end) + abs(sum(case when o.trade_status!='1' then final_point+5 end))))*10,2) as wenDing_fengXian")
                )
            ->where($where)
            ->where('m.username', 'like' , "%$searchname%")
            ->havingRaw('(SUM(o.final_point)+COUNT(o.id)*5) > 0')
            ->havingRaw('COUNT(o.id) > 10')
            ->orderBy($orderBy,$sort)
            ->orderBy('m.grade','desc')
            ->orderBy('single_final_point','desc')
            ->groupBy('m.tel')
            ->paginate(10);
        
        //dd($list);
        
        foreach ($list as $key=>$val){
            //是否订阅了该分析师
            if($session_data){
                $fllowinfo = DB::table('waihui_follow')->select('status','direction')->where([['analyst_id','=',$val->id],['follower_id','=',$session_data['id']]])->get()->toArray();
                if($fllowinfo){
                    if($fllowinfo[0]->status){
                        $list[$key]->status    = 1;//已订阅
                        $list[$key]->direction = $fllowinfo[0]->direction;//订阅方向
                    }else{
                        $list[$key]->status    = 0;//取消订阅
                        $list[$key]->direction = 0;//取消订阅
                    }
                }else{
                    $list[$key]->status    = 3;//未订阅
                    $list[$key]->direction = 3;//未订阅
                }
            }else{
                $list[$key]->status    = 4; //未登录
                $list[$key]->direction = 4; //未登录
            }
        }
        $uid = $session_data['id'];
        $userinfo = DB::table('waihui_member as m')
                        ->select('m.username','m.sex','m.email','m.birthday','m.server_name','m.tel')
                        ->where('m.id','=',$uid)
                        ->first();
        if(!$userinfo){
            $infofinish = 1; //未完善资料
        }else{
            $userinfos = DB::table('waihui_member_info as i')->select('i.province','i.city','i.country')->where('i.userid','=',$uid)->first();
            if($userinfo->username  && $userinfo->sex  && $userinfo->email && $userinfo->birthday  && $userinfo->server_name  && $userinfos->province && $userinfos->city && $userinfos->country ){
                $infofinish = 0;  //已完善资料
            }else{
                $infofinish = 1; //未完善资料
            }
        }
        // dd($list);
        
        $totalbaobiao = $this->allOrder();
        $totalbbtime = $totalbaobiao[0];//报表时间
        $totalbbpoint = $totalbaobiao[1];//报表收益
        $totalbballpoint = $totalbaobiao[2];//报表总收益点
        // dd($list);
        if(isMobile()){
            return view('newmobile.wendingzhishu',compact('list','searchname','type','grade','bdtype','infofinish','totalbbtime','totalbbpoint','totalbballpoint','pagecanshu','wenDing_fengXian'));
        }else{
            return view('new.wendingzhishu',compact('list','searchname','type','grade','bdtype','infofinish','totalbbtime','totalbbpoint','totalbballpoint','pagecanshu','wenDing_fengXian'));
        }
  }
    // 风报比
    public function fengBaoBi(Request $request){
        $pagecanshu = trim($request->pagecanshu);
        if($pagecanshu == ''){
            $pagecanshu = 1;
        }
        $searchname   = trim($request->searchname);
        $searchname   = $searchname?$searchname:"";
//        $where['isanalyst']    = '1';
        $orderBy  = 'wenDing_fengXian';
        $sort     = 'desc';
        
        
        $where['o.ispersonal'] = 0; //网站下单的
        $where['o.isstop']     = 0; //已平仓的
        $session_data = $request->session()->get('eai');

        $list = DB::table('waihui_member as m')
            ->join('waihui_order as o','m.tel','=','o.tel')
            ->select('m.authstatus','m.id','m.grade','m.tel','m.money','m.username','m.isanalyst','m.photo','m.star',
                    DB::raw("(SUM(o.final_point)+COUNT(o.id)*5) AS final_point"),
                    DB::raw("((SUM(o.final_point)+COUNT(o.id)*5)/count(o.tel)) AS single_final_point"),
                    DB::raw("(count(case when o.trade_status='1' then 1 end) / count(o.tel))*100 AS winrate"),
                    DB::raw("count(case when o.trade_status='1' then 1 end) AS wincounts"),
                    DB::raw("count(o.tel) as counts"),
                    DB::raw("round((sum(case when o.trade_status='1' then final_point+5 end)/count(case when o.trade_status='1' then 1 end))/(abs(sum(case when o.trade_status!='1' then final_point+5 end))/count(case when o.trade_status!='1' then 1 end)),2) as wenDing_fengXian")
                )
            ->where($where)
            ->where('m.username', 'like' , "%$searchname%")
            ->havingRaw('(SUM(o.final_point)+COUNT(o.id)*5) > 0')
            ->havingRaw('COUNT(o.id) > 10')
            ->orderBy($orderBy,$sort)
            ->orderBy('m.star','desc')
            ->orderBy('single_final_point','desc')
            ->groupBy('m.tel')
            ->paginate(10);
        //dd($list);
        /*$sql =DB::getQueryLog();
        dd($sql);*/
        foreach ($list as $key=>$val){
            
            //是否订阅了该分析师
            if($session_data){
                $fllowinfo = DB::table('waihui_follow')->select('status','direction')->where([['analyst_id','=',$val->id],['follower_id','=',$session_data['id']]])->get()->toArray();
                if($fllowinfo){
                    if($fllowinfo[0]->status){
                        $list[$key]->status    = 1;//已订阅
                        $list[$key]->direction = $fllowinfo[0]->direction;//订阅方向
                    }else{
                        $list[$key]->status    = 0;//取消订阅
                        $list[$key]->direction = 0;//取消订阅
                    }
                }else{
                    $list[$key]->status    = 3;//未订阅
                    $list[$key]->direction = 3;//未订阅
                }
            }else{
                $list[$key]->status    = 4; //未登录
                $list[$key]->direction = 4; //未登录
            }
        }
//      dd($list);
        $uid = $session_data['id'];
        $userinfo = DB::table('waihui_member as m')
                        ->select('m.username','m.sex','m.email','m.birthday','m.server_name','m.tel')
                        ->where('m.id','=',$uid)
                        ->first();
        if(!$userinfo){
            $infofinish = 1; //未完善资料
        }else{
            $userinfos = DB::table('waihui_member_info as i')->select('i.province','i.city','i.country')->where('i.userid','=',$uid)->first();
            if($userinfo->username  && $userinfo->sex  && $userinfo->email && $userinfo->birthday  && $userinfo->server_name  && $userinfos->province && $userinfos->city && $userinfos->country ){
                $infofinish = 0;  //已完善资料
            }else{
                $infofinish = 1; //未完善资料
            }
        }
        // dd($list);
        
        $totalbaobiao = $this->allOrder();
        $totalbbtime = $totalbaobiao[0];//报表时间
        $totalbbpoint = $totalbaobiao[1];//报表收益
        $totalbballpoint = $totalbaobiao[2];//报表总收益点
        if(isMobile()){
           return  view('newmobile.fengbaobi',compact('list','searchname','type','grade','bdtype','infofinish','totalbbtime','totalbbpoint','totalbballpoint','pagecanshu','wenDing_fengXian'));
       }else{
           return view('new.fengbaobi',compact('list','searchname','type','grade','bdtype','infofinish','totalbbtime','totalbbpoint','totalbballpoint','pagecanshu','wenDing_fengXian'));
       }
  }
    // 胜率榜
    public function shengLvBang(Request $request){
        $pagecanshu = trim($request->pagecanshu);
        if($pagecanshu == ''){
            $pagecanshu = 1;
        }
        $searchname   = trim($request->searchname);
        $searchname   = $searchname?$searchname:"";
//        $where['isanalyst']    = '1';
        $orderBy  = 'winrate';
        $sort     = 'desc';
        $end_start_time = 0;
        $where['o.ispersonal'] = 0; //网站下单的
        $where['o.isstop']     = 0; //已平仓的
        $session_data = $request->session()->get('eai');
        /*DB::connection()->enableQueryLog();
        DB::table('waihui_member');*/
        $list = DB::table('waihui_member as m')
            ->join('waihui_order as o','m.tel','=','o.tel')
            ->select('m.authstatus','m.id','m.grade','m.tel','m.money','m.username','m.isanalyst','m.photo','m.star',DB::raw("(SUM(o.final_point)+COUNT(o.id)*5) AS final_point"),DB::raw("((SUM(o.final_point)+COUNT(o.id)*5)/count(o.tel)) AS single_final_point"),DB::raw("(count(case when o.trade_status='1' then 1 end) / count(o.tel))*100 AS winrate"),DB::raw('count(o.tel) as ordernum'),DB::raw("count(case when o.trade_status='1' then 1 end) AS wincounts"),DB::raw("count(o.tel) as counts"))
            ->where($where)
            ->whereBetween('o.end_time', [$end_start_time, strtotime('now')])
            ->where('m.username', 'like' , "%$searchname%")
            ->havingRaw('(SUM(o.final_point)+COUNT(o.id)*5) > 0')
            ->havingRaw('COUNT(o.id) > 10')
            ->orderBy($orderBy,$sort)
            ->orderBy('m.star','desc')
            ->orderBy('single_final_point','desc')
            ->groupBy('m.tel')
            ->paginate(10);
        /*$sql =DB::getQueryLog();
        dd($sql);*/
        foreach ($list as $key=>$val){
            $list[$key]->weekordernum = DB::table('waihui_order')->where([['tel','=',$val->tel],['isstop','=',0],['begin_time','>',strtotime("-1 week")],['begin_time','<',strtotime("now")]])->count();//近一周的策略总数
            $list[$key]->monthordernum = DB::table('waihui_order')->where([['tel','=',$val->tel],['isstop','=',0],['begin_time','>',strtotime("-1 month")],['begin_time','<',strtotime("now")]])->count();//近一月的策略总数
            $list[$key]->fansnum      = BangdanController::followCount($val->id); //订阅该分析师的总数
            //获取图标中点位信息
            $trendpointobj            = DB::table('waihui_order')
                ->select(DB::raw('(SUM(final_point)+COUNT(id)*5) as final_point'),DB::raw("FROM_UNIXTIME(end_time, '%Y-%m-%d') AS days"))
                ->where([['tel','=',$val->tel],['ispersonal','=',0],['isstop','=',0],['end_time','>',$end_start_time],['end_time','<',strtotime("-1 day 23:59:59")]])
                ->groupBy('days')->get()->toArray();
            $timearr  = [];
            $pointarr = [];
            $pointcount = 0;
            foreach ($trendpointobj as $tkey=>$tval){
                $timearr[]  = $tval->days;
                $pointcount = $pointcount+$tval->final_point;
                $pointarr[] = round($pointcount,2);
            }
            $list[$key]->timesstr     = implode(',',$timearr);
            $list[$key]->pointstr     = implode(',',$pointarr);

            //是否订阅了该分析师
            if($session_data){
                $fllowinfo = DB::table('waihui_follow')->select('status','direction')->where([['analyst_id','=',$val->id],['follower_id','=',$session_data['id']]])->get()->toArray();
                if($fllowinfo){
                    if($fllowinfo[0]->status){
                        $list[$key]->status    = 1;//已订阅
                        $list[$key]->direction = $fllowinfo[0]->direction;//订阅方向
                    }else{
                        $list[$key]->status    = 0;//取消订阅
                        $list[$key]->direction = 0;//取消订阅
                    }
                }else{
                    $list[$key]->status    = 3;//未订阅
                    $list[$key]->direction = 3;//未订阅
                }
            }else{
                $list[$key]->status    = 4; //未登录
                $list[$key]->direction = 4; //未登录
            }
        }
//      dd($list);
        $uid = $session_data['id'];
        $userinfo = DB::table('waihui_member as m')
            ->select('m.username','m.sex','m.email','m.birthday','m.server_name')
            ->where('m.id','=',$uid)
            ->first();
        if(!$userinfo){
            $infofinish = 1; //未完善资料
        }else{
            $userinfos = DB::table('waihui_member_info as i')->select('i.province','i.city','i.country')->where('i.userid','=',$uid)->first();
            if($userinfo->username  && $userinfo->sex  && $userinfo->email && $userinfo->birthday  && $userinfo->server_name  && $userinfos->province && $userinfos->city && $userinfos->country ){
                $infofinish = 0;  //已完善资料
            }else{
                $infofinish = 1; //未完善资料
            }
        }
        $totalbaobiao = $this->allOrder();
        $totalbbtime = $totalbaobiao[0];//报表时间
        $totalbbpoint = $totalbaobiao[1];//报表收益
        $totalbballpoint = $totalbaobiao[2];//报表总收益点
        if(isMobile()){
            return  view('newmobile.shenglvbang',compact('list','searchname','type','grade','bdtype','infofinish','totalbbtime','totalbbpoint','totalbballpoint'));
        }else{
            return  view('new.shenglvbang',compact('list','searchname','infofinish','totalbbtime','totalbbpoint','totalbballpoint','pagecanshu'));
        }
    }
    
    public function lianShengInfo(Request $request){
        $tradeid = trim($request->tradeid);
        $tradeid = explode(',', $tradeid);
        //$tel    = DB::table('waihui_member')->where(array('id'=>$userid))->value('tel');
        if($tradeid){
            $trendpointobj = DB::table('waihui_order as o')
            			->join('waihui_type as t','t.name','o.typename')
                                ->select('t.chinaname','o.tradeid','o.typename','o.buy_status','o.begin_point','o.end_point','o.begin_time','o.end_time','o.final_point','o.target_profit_point','o.target_loss_point')
                                ->whereIn('tradeid',$tradeid)
                                ->orderBy('o.end_time','desc')->get()->toArray();
            $timearr  = [];
            $pointarr = [];
            $point = 0;
            $trendpointobj1 = array_reverse($trendpointobj);
            foreach ($trendpointobj1 as $tkey=>$tval){
                $timearr[]  = $tval->end_time;
                $point      = $point+$tval->final_point+5;
                $pointarr[] = $point;
            }
            $timesstr   = implode(',',$timearr);
            $pointstr   = implode(',',$pointarr);
            echo json_encode(array('status'=>0,'msg'=>'数据读取成功','timesstr'=>$timesstr,'pointstr'=>$pointstr,'trendpointobj'=>$trendpointobj));
        }else{
            echo json_encode(array('status'=>1,'msg'=>'用户信息不正确'));
        }

    }
    
    // 新反向榜
    public function fanXiangBang(Request $request){
        $session_data = $request->session()->get('eai');
        $uid = $session_data['id'];
        $userinfo = DB::table('waihui_member as m')
                        ->select('m.username','m.sex','m.grade','m.email','m.birthday','m.server_name')
                        ->where('m.id','=',$uid)
                        ->first();
        if(!$userinfo){
            $infofinish = 1; //未完善资料
        }else{
            $userinfos = DB::table('waihui_member_info as i')->select('i.province','i.city','i.country')->where('i.userid','=',$uid)->first();
            if($userinfo->username  && $userinfo->sex  && $userinfo->email && $userinfo->birthday  && $userinfo->server_name  && $userinfos->province && $userinfos->city && $userinfos->country ){
                $infofinish = 0;  //已完善资料
            }else{
                $infofinish = 1; //未完善资料
            }
        }

//        $weekstarttime  = strtotime(date("Y-m-d H:i:s",mktime(6,0,0,date("m"),date("d")-date("w")+(date("w")==0?-6:1),date("Y")))); //周一早上6:00:00
//        $weekendtime    = strtotime(date("Y-m-d H:i:s",mktime(12,0,0,date("m"),date("d")-date("w")+(date("w")==0?-1:6),date("Y")))); //周六早上5:00:00
        $endTime   = time();
        $beginTime = $endTime - 7*24*3600;
        //dd($endTime);
        $where['o.isstop']     = 0; //已平仓
        $where['o.ispersonal'] = 0; //网站下单的

        $searchname     = trim($request->searchname);
        $searchname     = $searchname?$searchname:"";
        $pagecanshu = trim($request->pagecanshu);
        if($pagecanshu == ''){
            $pagecanshu = 1;
        }
        $list = DB::table('waihui_member as m')
                        ->leftJoin('waihui_order as o', 'm.tel', 'o.tel')
                        ->select('m.id', 'm.tel', 'm.username', 'm.authstatus', 'm.photo','m.grade', DB::raw('(SUM(o.final_point)+COUNT(o.id)*5) as week_final_point'), DB::raw('count(o.tel) as ordernum'), DB::raw("((SUM(o.final_point)+COUNT(o.id)*5)/count(o.tel)) AS single_final_point"), DB::raw("(count(case when o.trade_status='1' then 1 end) / count(o.tel))*100 AS winrate"), DB::raw("ROUND(SUM(CASE WHEN o.trade_status='1' THEN (final_point+5) END),2) AS profit_final_point"),DB::raw("ROUND(SUM(CASE WHEN o.trade_status !='1' THEN (final_point+5) END),2) AS loss_final_point"))
                        ->where($where)
                        ->where('m.username', 'like' , "%$searchname%")
                        ->whereBetween('o.end_time', [$beginTime, $endTime])
                        ->orderBy('week_final_point', 'asc')
                        ->orderBy('single_final_point', 'asc')
                        ->groupBy('m.id')
                        ->paginate(10);
        // dd($list);
        foreach ($list as $key=>$val){
            // 盈亏比
            $list[$key]->bili = ($val -> profit_final_point == null ? 0:$val -> profit_final_point).' : '.($val-> loss_final_point == null? 0:$val-> loss_final_point);
            //获取图标中点位信息
            $trendpointobj            = DB::table('waihui_order')
                                            ->select(DB::raw('(final_point+5) as final_point'),'end_time')
                                            ->where([['tel','=',$val->tel],['ispersonal','=',0],['isstop','=',0],['end_time','>=',$beginTime],['end_time','<=',$endTime]])
                                            ->orderBy('end_time','desc')->get()->toArray();
            $timearr  = [];
            $pointarr = [];
            $pointcount = 0;
            foreach ($trendpointobj as $tkey=>$tval){
                $timearr[]  = date('Y-m-d',$tval->end_time);
                $pointcount = $pointcount+$tval->final_point;
                $pointarr[] = round($pointcount,2);
            }
            $list[$key]->timesstr     = implode(',',$timearr);
            $list[$key]->pointstr     = implode(',',$pointarr);
            
            //是否订阅了该分析师
            if($session_data){
                $fllowinfo = DB::table('waihui_follow')->select('status','direction')->where([['analyst_id','=',$val->id],['follower_id','=',$session_data['id']]])->get()->toArray();
                if($fllowinfo){
                    if($fllowinfo[0]->status){
                        $list[$key]->status    = 1;//已订阅
                        $list[$key]->direction = $fllowinfo[0]->direction;//订阅方向
                    }else{
                        $list[$key]->status    = 0;//取消订阅
                        $list[$key]->direction = 0;//取消订阅
                    }
                }else{
                    $list[$key]->status    = 3;//未订阅
                    $list[$key]->direction = 3;//未订阅
                }
            }else{
                $list[$key]->status    = 4; //未登录
                $list[$key]->direction = 4; //未登录
            }
        }
// dd($list);
        $totalbaobiao = $this->allOrder();
        $totalbbtime = $totalbaobiao[0];//报表时间
        $totalbbpoint = $totalbaobiao[1];//报表收益
        $totalbballpoint = $totalbaobiao[2];//报表总收益点
        if(isMobile()) {
            return view('newmobile.fanxiangbang',compact('infofinish','totalbaobiao','totalbbtime','totalbbpoint','totalbballpoint','list','searchname','pagecanshu'));
        }else{
            return view('new.fanxiangbang',compact('infofinish','totalbaobiao','totalbbtime','totalbbpoint','totalbballpoint','list','searchname','pagecanshu'));
        }
  }
  
  //比赛排名,点击查看个人详细策略情况
    public function showRankingInfo(Request $request){
        $userid = trim($request->userid);
        $tel    = DB::table('waihui_member')->where(array('id'=>$userid))->value('tel');
        if($tel){
            $end_time   = time();
            $start_time = $end_time - 7*24*3600;
            $trendpointobj = DB::table('waihui_order as o')
            			->join('waihui_type as t','t.name','o.typename')
                                ->select('t.chinaname','o.tradeid','o.typename','o.buy_status','o.begin_point','o.end_point','o.begin_time','o.end_time','o.final_point','o.target_profit_point','o.target_loss_point')
                                ->where([['o.tel','=',$tel],['o.ispersonal','=',0],['o.isstop','=',0],['o.end_time','>',$start_time],['o.end_time','<',$end_time]])
                                ->orderBy('o.end_time','desc')->get()->toArray();
            $timearr  = [];
            $pointarr = [];
            $point = 0;
            $trendpointobj1 = array_reverse($trendpointobj);
            foreach ($trendpointobj1 as $tkey=>$tval){
                $timearr[]  = $tval->end_time;
                $point      = $point+$tval->final_point+5;
                $pointarr[] = $point;
            }
            $timesstr   = implode(',',$timearr);
            $pointstr   = implode(',',$pointarr);
            echo json_encode(array('status'=>0,'msg'=>'数据读取成功','timesstr'=>$timesstr,'pointstr'=>$pointstr,'trendpointobj'=>$trendpointobj));
        }else{
            echo json_encode(array('status'=>1,'msg'=>'用户信息不正确'));
        }
    }
    // 新手机端查看详情页
    public function showInfo(Request $request){
        $userid = trim($request->id);
    	return view('newmobile.showinfo',compact('userid'));
    }
    
     public function showDetailInfo(Request $request){ //喊单详细记录
	$pagesize  = 10;
        $userid = trim($request->analyst_id);
        $tel    = DB::table('waihui_member')->where(array('id'=>$userid))->value('tel');
        $page      = intval(trim($request->page)) ? intval(trim($request->page)) :1;
        $offset    = ($page-1)*$pagesize;
        $end_time   = time();
        //$start_time = $end_time - 7*24*3600;
        $start_time = 0;
        $data['page'] = $page;
        $data['allnum'] =  DB::table('waihui_order as o')
                        ->where([['o.tel','=',$tel],['o.ispersonal','=',0],['o.isstop','=',0],['o.end_time','>',$start_time],['o.end_time','<',$end_time]])
                        ->count('id');
        $data['allpage'] = ceil($data['allnum']/$pagesize);
        if($tel){
            $data['arr']  = DB::table('waihui_order as o')
                            ->join('waihui_type as t','t.name','o.typename')
                            ->select('t.chinaname','o.tradeid','o.typename','o.buy_status','o.begin_point','o.end_point','o.begin_time','o.end_time','o.final_point','o.target_profit_point','o.target_loss_point')
                            ->where([['o.tel','=',$tel],['o.ispersonal','=',0],['o.isstop','=',0],['o.end_time','>',$start_time],['o.end_time','<',$end_time]])
                            ->orderBy('o.end_time','desc')
                            ->offset($offset)
                            ->limit($pagesize)
                            ->get()->toArray();
        }
        //dd($data);
        return json_encode($data);
    }
    // 推荐榜
    public function tuiJianBang(Request $request){
        return view('newmobile.tuijianbang');
    }
}
